<template>
  <div id="login">
    <el-form 
      :model="form" 
      :rules="rules" 
      status-icon
      label-width="100px"
      ref="form"
      class="login-container"
    >
      <h3 class="login-title">系统登录</h3>
      <el-form-item
        label="用户名"
        label-width="80px"
        prop="username"
        class="username"
      >
      <el-input
        type="input"
        v-model="form.username"
        placeholder="请输入账号"
      ></el-input>
      </el-form-item>
      <el-form-item
        label="密码"
        label-width="80px"
        prop="password"
        class="password"
      >
      <el-input
        type="input"
        v-model="form.password"
        placeholder="请输入密码"
      ></el-input>
      </el-form-item>
      <el-form-item class="login-submit-wrapper">
        <el-button type="primary" @click="login('form')" class="login-submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    var validateUsername = (rule, value, callback) => {
      if(!value) {
        return callback(new Error('用户名不能为空'))
      }
      callback()
    }
    var validatePassword = (rule, value, callback) => {
      if(!value) {
        return callback(new Error('密码不能为空'))
      }
      callback()
    }
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { validator: validateUsername, require: true, message: '请输入正确的用户名', trigger: 'blur' },
          { min: 3, message: '用户名长度不能少于3位', trigger: 'blur'}
        ],
        password: [
          { validator: validatePassword, require: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 3, message: '密码长度不能少于3位', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login(formName) {
      // 判断合法性
      this.$refs[formName].validate((valid, object) => {
        if (valid) {
          // 模拟账号密码验证
          if(this.form.username === 'admin' && this.form.password === 'admin') {
            // 显示登录成功
            this.$notify({
              title: '成功',
              message: '登录成功',
              type: 'success'
            })
            // 清除token
            this.$store.commit('clearToken')
            // 模拟发送登录请求 获取token 保存token
            const token = 'thisIsFakeToken'
            this.$store.commit('setToken', token)
            this.$router.push({name:'home'})
          }else {
            // 账号或密码错误
            this.$notify.error({
              title: '失败',
              message: '账号或密码错误',
            });
          }
        } else {
          this.$notify({
            title: '提示',
            message: '请输入账号和密码',
            type: 'warning'
          })
          return false;
        }
      });
    }
  },
}
</script>

<style lang="less" scoped>
#login {
  width: 350px;
  background-color: #fff;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  background-clip: padding-box;
  border: 1px solid #eaeaea;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
}
.login-title {
  color: #505458;
  text-align: center;
  margin: 0 auto 40px;
}
.login-submit-wrapper {
  display: flex;
  justify-content: center;
  .login-submit {
    transform: translateX(-50%);
  }
}
</style>